package cn.bytecollege.user.controller;

import cn.bytecollege.common.entity.Address;
import cn.bytecollege.common.entity.User;
import cn.bytecollege.user.service.AddressService;
import com.mysql.cj.Session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "AddressSaveController",value = "/user/addAddress")
public class AddressSaveController extends HttpServlet {
    AddressService addressService=null;
    public AddressSaveController(){
        addressService=new AddressService();
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    AddressService addressService=new AddressService();
    HttpSession session=request.getSession();
    Address address=new Address();
    //1.封装数据
    String addressConsignee=request.getParameter("Consignee");
    String addressPhonenumber=request.getParameter("Phonenumber");
    String addressProvince=request.getParameter("province");
    String addressCity=request.getParameter("city");
    String addressCounty=request.getParameter("county");
    String addressProfile=request.getParameter("Profile");
    User user=(User) session.getAttribute("user");
    address.setAddressConsignee(addressConsignee);
    address.setAddressProvince(addressProvince);
    address.setAddressPhonenumber(addressPhonenumber);
    address.setAddressCity(addressCity);
    address.setAddressCounty(addressCounty);
    address.setAddressProfile(addressProfile);
    address.setUserId(user.getUserId());
    addressService.save(address);
    response.sendRedirect("/addAddress");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     request.getRequestDispatcher("/addAddress.jsp").forward(request,response);
    }
}
